home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 April: Mac OS SDK / Dev.CD Apr 98 SDK1.toast / Development Kits (Disc 1) / Apple Shared Library Manager / ASLM Examples / Example Tools / Sources / TArbitratorExample1.cp < prev    next >
Encoding:
Text File  |  1996-11-19  |  2.4 KB  |  73 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        TArbitratorExample1.cp
  3.  
  4.     Contains:    This module show an example use of the TArbitrator class.
  5.  
  6.     Copyright:    © 1993 by Apple Computer, Inc., all rights reserved.
  7.  
  8. */
  9.  
  10. #include "TInitSLM.h"            // the TInitSLM class and SLM include files
  11.  
  12. ///————————————————————————————————————————————————————————————————————————————————————
  13. ///    CONSTANTS
  14. ///————————————————————————————————————————————————————————————————————————————————————
  15.  
  16. // id of the object we register with the arbitrator
  17. #define kLinkObjectID    "slm:exam$link1"
  18.  
  19. /*————————————————————————————————————————————————————————————————————————————————————
  20.     main 
  21.     
  22.     This is a simple demonstration of how to use the TArbitrator class. It creates a
  23.     new arbitrator and registers an object with it. Once the object is registered
  24.     other clients having access to the same TArbitrator can lookup the object if they
  25.     know the ID of that the object was registered with. Once the ID and the arbitrator
  26.     is known the client can lookup the object by calling "LookupObject".
  27. ————————————————————————————————————————————————————————————————————————————————————*/
  28.  
  29. main() 
  30. {    
  31.     TInitSLM initLibraryManager;                // initialize the shared library manager
  32.     
  33.     if( initLibraryManager.Failed() )        // If we failed, let's go home
  34.         return 1;
  35.         
  36.     TArbitrator *arbitrator;
  37.     
  38.     if( arbitrator = new TArbitrator ) {    // create our own arbitrator
  39.         
  40.         cout << "Got a new arbitrator" << endl;
  41.         
  42.         TLink *link;
  43.                 
  44.         if( link = new TLink ) {             // This is the object we are arbitrating
  45.             
  46.             cout <<"Allocating and registering an object…" << endl;
  47.             
  48.             // Register the object so other clients can look it up
  49.             OSErr err = arbitrator->RegisterObject( kLinkObjectID, (void *)link );
  50.             if( err != kNoError)
  51.                 cout << "ERROR ==> {RegisterObject} = " << err << endl;
  52.             else {
  53.                 // see if we can find the object by ID
  54.                 TLink *object = (TLink *)arbitrator->LookupObject( kLinkObjectID );
  55.                 
  56.                 if( object != link )        // if object returned != our object
  57.                     cout << "Something fishy, we got the wrong object" << endl;
  58.                 else
  59.                     cout << "LookupObject returned our Registered object" << endl;
  60.  
  61.                 // since we are the only one using this object we can go ahead
  62.                 // unregister and then delete the object
  63.                 if( object = (TLink *)arbitrator->UnregisterObject( kLinkObjectID ) )
  64.                     cout << "Unregistered the object OK" << endl;
  65.             }
  66.             delete link;                    // we are done w/ the object
  67.         }
  68.         delete arbitrator;                    // and done with arbitrator
  69.     }
  70.     
  71.     return 0;
  72. }
  73.